package app;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Login.java
 *
 * Created on Aug 14, 2011, 2:54:55 PM
 */
import java.awt.Font;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import profiles.ProfilesDefault;

/**
 *
 * @author yoshi
 */
public class Login extends javax.swing.JFrame {

    /** Creates new form Login */
    public Login() {
        initComponents();

        this.setResizable(false);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        txtUsername = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        btnLogin = new javax.swing.JButton();
        btnExit = new javax.swing.JButton();
        txtPassword = new javax.swing.JPasswordField();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Username:");

        jLabel2.setText("Password:");

        btnLogin.setText("Login");
        btnLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLoginActionPerformed(evt);
            }
        });

        btnExit.setText("Exit");
        btnExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExitActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(44, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addGap(27, 27, 27)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtPassword)
                            .addComponent(txtUsername, javax.swing.GroupLayout.PREFERRED_SIZE, 188, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(53, 53, 53)
                        .addComponent(btnLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnExit, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(37, 37, 37))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(txtUsername, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(txtPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnExit)
                    .addComponent(btnLogin))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExitActionPerformed
        this.dispose();
    }//GEN-LAST:event_btnExitActionPerformed

    private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLoginActionPerformed
        final Login self = this;

        new Thread() {

            @Override
            public void run() {
                btnLogin.setEnabled(false);

                if (Auth.verify(txtUsername.getText(), new String(txtPassword.getPassword()))) {
                    Home home = new Home();
                    Global.setFrame(home);
                    home.setVisible(true);
                    home.load(new ProfilesDefault());
                    self.dispose();
                } else {
                    btnLogin.setEnabled(true);
                    JOptionPane.showMessageDialog(self, "Login failed, please try again");
                }
            }
        }.start();

    }//GEN-LAST:event_btnLoginActionPerformed

    /*
     * Function to get static information from database, set enrionments variables ...
     * 
     */
    private static void bootstrap() {    
        new Thread() {

            @Override
            public void run() {
                Connection connection = Global.getConnection();
                Global.loadDepartments(connection);
                Global.loadRolls(connection);
                Global.loadTimeSlots(connection);

                try {
                    connection.close();
                } catch (SQLException ex) {
                    Logger.getLogger(Auth.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }.start();
        
        try {
            UIManager.setLookAndFeel("ch.randelshofer.quaqua.QuaquaLookAndFeel15");
        } catch (ClassNotFoundException ex) {
        } catch (InstantiationException ex) {
        } catch (IllegalAccessException ex) {
        } catch (UnsupportedLookAndFeelException ex) {
        }

        Font verdana = new Font("Verdana", 0, 12);
        UIManager.put("Button.font", verdana);
        UIManager.put("ToggleButton.font", verdana);
        UIManager.put("RadioButton.font", verdana);
        UIManager.put("CheckBox.font", verdana);
        UIManager.put("ColorChooser.font", verdana);
        UIManager.put("ComboBox.font", verdana);
        UIManager.put("Label.font", verdana);
        UIManager.put("List.font", verdana);
        UIManager.put("MenuBar.font", verdana);
        UIManager.put("MenuItem.font", verdana);
        UIManager.put("RadioButtonMenuItem.font", verdana);
        UIManager.put("CheckBoxMenuItem.font", verdana);
        UIManager.put("Menu.font", verdana);
        UIManager.put("PopupMenu.font", verdana);
        UIManager.put("OptionPane.font", verdana);
        UIManager.put("Panel.font", verdana);
        UIManager.put("ProgressBar.font", verdana);
        UIManager.put("ScrollPane.font", verdana);
        UIManager.put("Viewport.font", verdana);
        UIManager.put("TabbedPane.font", verdana);
        UIManager.put("Table.font", verdana);
        UIManager.put("TableHeader.font", verdana);
        UIManager.put("TextField.font", verdana);
        UIManager.put("PasswordField.font", verdana);
        UIManager.put("TextArea.font", verdana);
        UIManager.put("TextPane.font", verdana);
        UIManager.put("EditorPane.font", verdana);
        UIManager.put("TitledBorder.font", verdana);
        UIManager.put("ToolBar.font", verdana);
        UIManager.put("ToolTip.font", verdana);
        UIManager.put("Tree.font", verdana);
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        bootstrap();

        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Login().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnExit;
    private javax.swing.JButton btnLogin;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPasswordField txtPassword;
    private javax.swing.JTextField txtUsername;
    // End of variables declaration//GEN-END:variables
}
